home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / ibm / sym_r3.arc / SIM_R3.C < prev    next >
C/C++ Source or Header  |  1991-11-27  |  9KB  |  386 lines

  1. /***********************
  2.     Program: 68705R3 simulator
  3.     File: sim_r3 - main program / user interface.
  4.     Notes: This program is written in 'plain vanilla' C and should easily
  5.     port to any computer that has rudimentary control of its cursor
  6.     position.  Using defines in SIM.H, it has been successfully run
  7.     on IBM-PC compiled with Microsoft C 5.0, and on an Atari 520ST
  8.     compiled with Mark Williams C 3.0.  The 520ST emulates a DEC VT52
  9.     for cursor control.  It should be easy to adapt this code to drive
  10.     other terminals/emulations, ie, VT100, ADM-3, etc.
  11.     Changes:
  12.     1.21 - 8 July 1989 - Added clear of external interrupt latch to
  13.         sim_reset() in instr.c - This is to agree with 68705 reset.
  14.         The only symptom of this in rev 1.2 would be if someone
  15.         issues 'I' (interrupt) and then 'X' (reset) without
  16.         servicing the interrupt, the interrupt would persist. 
  17.  
  18. ***********************/
  19. #include <stdio.h>
  20.  
  21. #define SIMR3_C
  22. #include "sim.h"
  23.  
  24. #ifdef MSC
  25. #include <graph.h>
  26. #include <conio.h>
  27. #include <ctype.h>
  28. #endif
  29.  
  30. int display_enabled, log_enabled, promptline_full;
  31. int external_intr, timer_external_input;
  32. int disassemble = 0;
  33. long int cycle_count;
  34.  
  35. int trace_ena = 0;
  36. int breakaddr;
  37.  
  38. FILE *logfile;
  39.  
  40. unsigned int sym_nmbr[ 4095];
  41. char sym_tbl[ 1023];
  42.  
  43. int sim_halt()
  44. /* called from here and from instr.c on invalid op code */
  45. {
  46.     trace_ena = 0;
  47.     settextposition( 1, 65);
  48.     printf( "Halted ");
  49.     fflush(stdout);
  50. }
  51.  
  52. main()
  53. {
  54.     int exitflag = 0;
  55.     char cmdbuff[100];
  56.     char rspbuff[100];
  57.  
  58.     display_enabled = 1;
  59.     log_enabled = 1;
  60.     logfile = fopen( "SIM_R3.LOG", "w");
  61.     promptline_full = 0;
  62.     external_intr = 0;       /* latched /INT ; 0 = no INT latched */
  63.     timer_external_input = 0;
  64.  
  65.     clearscreen();
  66.     settextposition(1, 1);
  67.     printf( "68705R3 Simulator V1.21  8-July-1989\n" );
  68.     printf(
  69. "R - disp/chg regs    L file - load Sx & reset    E - toggle TIE  G - Go\n");
  70.     printf(
  71. "T - trace on/off    B addr - Break at address (go)  I - /INT      S - Step\n");
  72.     printf(
  73. "X - Reset      M addr - Mem view/mod   Z - Disassem     H - Halt      Q - Quit\n");
  74.  
  75. /* use command line for symbol table feature */
  76.     printf(
  77. "                                                        Y - load symbol table\n");
  78.  
  79.     settextposition( PROMPTLINE+1, 1);
  80.     printf(
  81. "______________________________________________________________________________"
  82.     );
  83.  
  84.     settextposition( 1, 50);
  85.     printf("Disp+log ON ");
  86.     sim_halt();
  87.     clearline(PROMPTLINE);
  88.     settextposition( PROMPTLINE, 1);
  89.  
  90.  
  91.     /* here is the main loop */
  92.     while (!exitflag)
  93.     {
  94.     /* Check for keyboard input */
  95.     if (kbhit())
  96.     {
  97.         char cmdchar;
  98.         char tempchar;
  99.  
  100.         clearline(PROMPTLINE);
  101.         settextposition( PROMPTLINE, 1);
  102.         fgets( cmdbuff, 80, stdin);
  103.         cmdchar = tolower( cmdbuff[0]);
  104.         if (cmdchar == 'r')
  105.         {
  106.         sprintf( rspbuff,
  107.         "PC= %03x, A= %02x, X= %02x, SP= %03x, CCR=%02x (",
  108.             pgm_model.pc, pgm_model.a, pgm_model.x, pgm_model.sp,
  109.             pgm_model.ccr);
  110.         if (pgm_model.ccr & CC_H)
  111.             strcat( rspbuff, "H");
  112.         if (pgm_model.ccr & CC_I)
  113.             strcat( rspbuff, "I");
  114.         if (pgm_model.ccr & CC_N)
  115.             strcat( rspbuff, "N");
  116.         if (pgm_model.ccr & CC_Z)
  117.             strcat( rspbuff, "Z");
  118.         if (pgm_model.ccr & CC_C)
  119.             strcat( rspbuff, "C");
  120.         strcat( rspbuff, ")  Change?(reg) ");
  121.         showprompt( rspbuff);
  122.         fgets( cmdbuff, 80, stdin);
  123.         tempchar = tolower( cmdbuff[0]);
  124.         if (tempchar == 'p')
  125.         {
  126.             showprompt("Enter new PC: ");
  127.             fgets( cmdbuff, 80, stdin);
  128.             sscanf( cmdbuff, "%x", &pgm_model.pc);
  129.         }
  130.         if (tempchar == 's')
  131.         {
  132.             showprompt("Enter new SP: ");
  133.             fgets( cmdbuff, 80, stdin);
  134.             sscanf( cmdbuff, "%x", &pgm_model.sp);
  135.         }
  136.         if (tempchar == 'a')
  137.         {
  138.             showprompt("Enter new A: ");
  139.             fgets( cmdbuff, 80, stdin);
  140.             sscanf( cmdbuff, "%x", &pgm_model.a);
  141.         }
  142.         if (tempchar == 'x')
  143.         {
  144.             showprompt("Enter new X: ");
  145.             fgets( cmdbuff, 80, stdin);
  146.             sscanf( cmdbuff, "%x", &pgm_model.x);
  147.         }
  148.         if (tempchar == 'c')
  149.         {
  150.             showprompt("Enter new CCR (in hex): ");
  151.             fgets( cmdbuff, 80, stdin);
  152.             sscanf( cmdbuff, "%x", &pgm_model.ccr);
  153.         }
  154.         clearline( PROMPTLINE);
  155.         }
  156.         if (cmdchar == 'l')
  157.         load( &cmdbuff[1]); /* skip command char */
  158.  
  159. /* Symbol table load here */
  160.         if (cmdchar == 'y')
  161.         load_sym( &cmdbuff[1]); /* skip command char */
  162.  
  163.         if (cmdchar == 'e')
  164.         {
  165.         timer_external_input = (timer_external_input)? 0 : 1;
  166.         sprintf(rspbuff, "Timer ext input is now %1d",
  167.                                     timer_external_input);
  168.         showprompt( rspbuff);
  169.         }
  170.         if (cmdchar == 't')
  171.         {
  172.         display_enabled = (display_enabled) ? 0 : 1;
  173.         if (display_enabled)
  174.         {
  175.             showprompt("Enable logfile also? (Y/N): ");
  176.             fgets( cmdbuff, 80, stdin);
  177.             settextposition( 1, 50);
  178.             if (cmdbuff[0] != 'n' && cmdbuff[0] != 'N')
  179.             {
  180.             printf("Disp+log ON ");
  181.             log_enabled = 1;
  182.             }
  183.             else
  184.             {
  185.             printf("Display ON  ");
  186.             log_enabled = 0;
  187.             }
  188.         }
  189.         else
  190.         {
  191.             settextposition( 1, 50);
  192.             printf("Disp+log OFF");
  193.         }
  194.         }
  195.         if (cmdchar == 'b' || cmdchar == 'g' || cmdchar == 's')
  196.         {
  197.         if (cmdchar == 'b')
  198.             sscanf( &cmdbuff[1], "%x", &breakaddr);
  199.         if (cmdchar != 's')
  200.             trace_ena = 1;
  201.         else
  202.             trace_ena = 2; /* step */
  203.         settextposition( 1, 65);
  204.         printf( "RUNNING");
  205.         }
  206.         if (cmdchar == 'i')
  207.         {
  208.             external_intr = 1;
  209.         showprompt("External interrupt latched");
  210.         }
  211.         if (cmdchar == 'x')
  212.         sim_reset();
  213.         if (cmdchar == 'm')
  214.         {
  215.         int addr, data, endm, numcvts;
  216.  
  217.         addr = 0x10;  /* Default */
  218.         sscanf( &cmdbuff[1], "%x", &addr);
  219.  
  220.         endm = 0;
  221.         while (!endm)
  222.         {
  223.             if (addr > 4095 || addr < 0)
  224.             addr = 0x10; /* Wrap to RAM = default */
  225.             data = sim_read( addr);
  226.             sprintf( rspbuff, "%03x %02x      ", addr, data);
  227.             showprompt(rspbuff);
  228.             settextposition( PROMPTLINE, 8); 
  229.             cmdbuff[0] = '\n';
  230.             fgets( cmdbuff, 80, stdin);
  231.             if (cmdbuff[0] != '\n')
  232.             {
  233.             numcvts = sscanf( cmdbuff, "%x", &data);
  234.             if (numcvts == 1)
  235.                 sim_writef( addr, data);
  236.             else
  237.                 endm = 1;
  238.             }
  239.             addr++;
  240.         }
  241.         clearline( PROMPTLINE);
  242.         }
  243.         if (cmdchar == 'h')
  244.         {
  245.             sim_halt();
  246.         }
  247.         if (cmdchar == 'q')
  248.         exitflag = 1;
  249.         if (cmdchar == 'z')
  250.         {
  251.         struct program_model saveregs;
  252.         int ic, addr, savelogena, savedispena;
  253.  
  254.         saveregs = pgm_model;
  255.         disassemble = 1;
  256.         savelogena = log_enabled;
  257.         savedispena = display_enabled;
  258.         log_enabled = 0;
  259.         display_enabled = 1;
  260.  
  261.         delete_line( PROMPTLINE+2);
  262.         settextposition( 49,1);
  263.         printf(" *** Disassembly follows ****");
  264.  
  265.         if (cmdbuff[1] != '\n')
  266.         {
  267.             ic = sscanf(&cmdbuff[1], "%x", &addr);
  268.             if (ic == 1)
  269.             pgm_model.pc = addr;
  270.         }
  271.  
  272.         for (ic = 0; ic < 40; ic++)
  273.         {
  274.             sim_instr();
  275.         }
  276.         delete_line( PROMPTLINE+2);
  277.         settextposition( 49,1);
  278.         printf (" *** End disassembly ****");
  279.  
  280.         disassemble = 0;
  281.         pgm_model = saveregs;
  282.         log_enabled =  savelogena;
  283.         display_enabled = savedispena;
  284.         }
  285.         if (cmdchar == '\n')
  286.         {
  287.         settextposition( PROMPTLINE+1, 1);
  288.         printf(
  289. "______________________________________________________________________________"
  290.         );
  291.         };
  292.         settextposition(PROMPTLINE, 1);
  293.     } /* end if kbhit */
  294.  
  295.     if (trace_ena)
  296.     {
  297.         instr_or_intr(); /* do an instruction */
  298.         if (pgm_model.pc == breakaddr)
  299.         {
  300.         sim_halt();
  301.         trace_ena = 0;
  302.         showprompt("BREAKPOINT reached");
  303.         }
  304.         if (trace_ena == 2)
  305.         {
  306.         sim_halt();
  307.         trace_ena = 0;
  308.         }
  309.     }
  310.     } /* end while !exitflag */
  311.     sim_halt();
  312.     fclose( logfile);
  313.     showprompt("Logfile closed - normal exit");
  314.     settextposition(25,1);
  315.     exit( 0);
  316. }
  317.  
  318.  
  319.     
  320.  
  321. showprompt( buff)
  322. char buff[];
  323. {
  324.     if (promptline_full)
  325.     clearline( PROMPTLINE);
  326.     settextposition( PROMPTLINE, 1);
  327.     printf( buff);
  328.     fflush(stdout);
  329.     promptline_full = 1;
  330. }
  331.  
  332. clearline( linenum)
  333. int linenum;
  334. {
  335.     settextposition( linenum, 1);
  336. #ifdef MSC
  337.     printf(
  338. "                                        ");
  339.     settextposition( linenum, 1);
  340. #endif
  341. #ifdef VT52
  342.     settextposition( linenum, 1);
  343.     printf( "\033K");    /* escape K - clear cursor pos through end of line */
  344.     settextposition( linenum, 1);
  345. #endif
  346.     if (linenum == PROMPTLINE)
  347.     promptline_full = 0;
  348. }
  349.  
  350. settextposition( line, col)
  351. int line,col;
  352. {
  353. #ifdef MSC
  354.     _settextposition( line, col);       /* Microsoft C 5.0 and up */
  355. #endif
  356. #ifdef VT52
  357.     printf("\033Y%c%c", 31+line, 31+col); /* escape Y row col  */
  358.     /* row and col are 0 to max with an offset of 32 */
  359.     fflush( stdout);
  360. #endif
  361. }
  362.  
  363. clearscreen()
  364. {
  365. #ifdef MSC
  366.     _clearscreen( _GCLEARSCREEN );       /* Microsoft C 5.0 and up */
  367. #endif
  368. #ifdef VT52
  369.     printf("\033E");  /* escape E - Clear & Home */
  370. #endif
  371. }
  372.  
  373. #ifdef VT52
  374. /* Assume this is for ATARI ST, which does VT52 emulation */
  375. /* We must supply our own version for the Microsoft C routine kbhit() */
  376.  
  377. #include <osbind.h>
  378.  
  379. int kbhit()
  380. {
  381.     return( Cconis() ); /* Check console input status */
  382. }
  383. #endif
  384.  
  385. /******************** end of file sim_r3.c ***************************/
  386.